home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
xlib
/
tri2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
7KB
|
272 lines
/*
* (c) Copyright 1993-94, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
#include <GL/glx.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/keysym.h>
static int attributes[] = {
GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
None,
};
int width = 200, height = 200;
GLUquadricObj *quadObj;
static void DoDisplay(GLfloat tx, GLfloat ty, GLfloat rx, GLfloat rz)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5, width - 0.5, -0.5, height - 0.5, 0.01, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(100 + tx, 100 + ty, -10);
glRotatef(rx, 1, 0, 0);
glRotatef(rz, 0, 0, 1);
glScalef(20, 20, 20);
glViewport(0, 0, width, height);
glClearColor(0.25, 0.25, 0.25, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_FLAT);
/* Draw a bunch of triangles that converge on a single point */
glBegin(GL_TRIANGLE_FAN);
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glVertex2f(-1, -0.5);
glVertex2f(-1, -0.25);
glColor3f(0, 1, 0);
glVertex2f(-1, 0);
glColor3f(0, 0, 1);
glVertex2f(-1, 0.25);
glColor3f(1, 1, 1);
glVertex2f(-1, 0.5);
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glVertex2f(-0.5, 1);
glVertex2f(-0.25, 1);
glColor3f(0, 1, 0);
glVertex2f(0, 1);
glColor3f(0, 0, 1);
glVertex2f(0.25, 1);
glColor3f(1, 1, 1);
glVertex2f(0.5, 1);
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glVertex2f(1, 0.5);
glVertex2f(1, 0.25);
glColor3f(0, 1, 0);
glVertex2f(1, 0);
glColor3f(0, 0, 1);
glVertex2f(1, -0.25);
glColor3f(1, 1, 1);
glVertex2f(1, -0.5);
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glVertex2f(-0.5, -1);
glVertex2f(-0.25, -1);
glColor3f(0, 1, 0);
glVertex2f(0, -1);
glColor3f(0, 0, 1);
glVertex2f(0.25, -1);
glColor3f(1, 1, 1);
glVertex2f(0.5, -1);
glEnd();
glPushMatrix();
gluQuadricDrawStyle (quadObj, GLU_FILL);
glColor3f (3.2, 0.8, 1.0);
gluDisk(quadObj,2.0,5.0,16,4);
}
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
{
if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
return GL_TRUE;
}
return GL_FALSE;
}
int main()
{
XVisualInfo *vi;
Display *dpy;
Colormap cmap;
Window window;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
GLboolean needDisplay;
GLfloat tx = 0;
GLfloat ty = 0;
GLfloat rx = 0;
GLfloat rz = 0;
dpy = XOpenDisplay(0);
if (!dpy) {
fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
return -1;
}
vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
if (!vi) {
fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
getenv("DISPLAY"));
return -1;
}
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
AllocNone);
swa.border_pixel = 0;
swa.colormap = cmap;
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
| KeyReleaseMask;
window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
width, height,
0, vi->depth, InputOutput, vi->visual,
CWBorderPixel|CWColormap|CWEventMask, &swa);
XSetWMColormapWindows(dpy, window, &window, 1);
XMapWindow(dpy, window);
XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
if (!glXMakeCurrent(dpy, window, cx)) {
fprintf(stderr, "Can't make window current to context\n");
return -1;
}
quadObj = gluNewQuadric ();
needDisplay = GL_TRUE;
for (;;) {
do {
XNextEvent(dpy, &event);
switch (event.type) {
case Expose:
needDisplay = GL_TRUE;
break;
case ConfigureNotify:
width = event.xconfigure.width;
height = event.xconfigure.height;
needDisplay = GL_TRUE;
break;
case KeyPress:
{
char buf[100];
int rv;
KeySym ks;
rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
switch (ks) {
case XK_p:
case XK_P:
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
needDisplay = GL_TRUE;
break;
case XK_l:
case XK_L:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
needDisplay = GL_TRUE;
break;
case XK_f:
case XK_F:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
needDisplay = GL_TRUE;
break;
case XK_Left:
tx -= 0.1;
needDisplay = GL_TRUE;
break;
case XK_Right:
tx += 0.1;
needDisplay = GL_TRUE;
break;
case XK_Up:
ty += 0.1;
needDisplay = GL_TRUE;
break;
case XK_Down:
ty -= 0.1;
needDisplay = GL_TRUE;
break;
case XK_KP_Left:
rz += 1;
needDisplay = GL_TRUE;
break;
case XK_KP_Right:
rz -= 1;
needDisplay = GL_TRUE;
break;
case XK_KP_Up:
rx += 1;
needDisplay = GL_TRUE;
break;
case XK_KP_Down:
rx -= 1;
needDisplay = GL_TRUE;
break;
case XK_Escape:
return 0;
}
}
break;
}
} while (XPending(dpy) != 0);
if (needDisplay) {
needDisplay = GL_FALSE;
DoDisplay(tx, ty, rx, rz);
}
}
}